package com.jeeplus.modules.statistics.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.ImmutableMap;
import com.jeeplus.common.web.PageResult;
import com.jeeplus.common.web.Result;
import com.jeeplus.database.datasource.annotation.DS;
import com.jeeplus.modules.statistics.common.SerialUtils;
import com.jeeplus.modules.statistics.mapper.ReserveMapper;
import com.jeeplus.modules.statistics.model.constant.Constant;
import com.jeeplus.modules.statistics.model.domain.ReserveCount;
import com.jeeplus.modules.statistics.model.query.ReserveQuery;
import com.jeeplus.modules.statistics.model.query.StatisticsQuery;
import com.jeeplus.modules.statistics.model.vo.ReserveVO;
import com.jeeplus.modules.statistics.model.vo.StatisticsXY;
import com.jeeplus.modules.sys.utils.UserUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;


/**
 * ClassName: GXQueueService
 * Function:  高新排队库
 * Date:      2022/7/4 10:53
 *
 * @author azj
 * version    V1.0
 */
@DS("swdata")
@Service
@Slf4j
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class GXQueueService {
    private final ReserveMapper reserveMapper;
    private final CounterService counterService;
    /**
     * 预约记录查询
     *
     * @param query
     * @return
     */
    public PageResult <List<ReserveVO>> queryReserve(ReserveQuery query) {
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        query.setUserid("%"+ UserUtils.getUser().getId()+"%");
        return new PageResult(new PageInfo<>(reserveMapper.selectBy(query)), HttpStatus.OK.value());
    }

    /**
     * 预约业务统计
     *
     * @param query
     * @return
     */
    public Result <StatisticsXY> reserveBusiness(StatisticsQuery query) {
        List<String> xValue = new ArrayList<>();
        List<Long> yValue = new ArrayList<>();
        try {
            List<ReserveCount> reserveCounts = reserveMapper.statistics(query);
            for (ReserveCount datum : reserveCounts) {
                StringBuilder xLabel = new StringBuilder();
                // 拼接x轴数据 例:社保服务[1号、2号]
                Optional.ofNullable(datum.getBusName()).ifPresent(xLabel::append);
                Optional.ofNullable(datum.getBusChar()).ifPresent(e -> xLabel.append("(").append(e).append(")"));
                // 根据业务id查询办理窗口号
                List<String> counterNo = counterService.findCounterNoBySerialNo(SerialUtils.buildSerial(datum.getSerial(), 3), query.getAreaId());
                String counterName = counterNo.stream().map(e -> e + "号").collect(Collectors.joining(" "));
                xLabel.append("[").append(counterName).append("]");
                xValue.add(xLabel.toString());
                yValue.add(datum.getNumber());
            }
            // 汇总
            long count = reserveCounts.stream().mapToLong(ReserveCount::getNumber).sum();
            return Result.succeed(new StatisticsXY(xValue, yValue), "获取成功！", ImmutableMap.of(Constant.AJAX_JSON_COUNT, count));
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return Result.failed("统计失败，请联系管理员！");
    }

    /**
     * 删除预约
     *
     * @param id
     * @return
     */
    public Boolean deleteReserve(String id) {
        return reserveMapper.updateStatus(id) > 0;
    }

}